﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeiXinPF.Common;
using System.Text;
using System.Data;
namespace WeiXinPF.Web.shop
{
    /// <summary>
    /// shop 的摘要说明精品商城
    /// </summary>
    public class shop : IHttpHandler
    {
        /// <summary>
        /// slf修改增加过滤精品商城选项
        /// </summary>
        string condition = string.Format(" and  catalogId={0} ", System.Configuration.ConfigurationManager.AppSettings["ShopProductCatalog"]);
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            try
            {
                BLL.wx_shop_cart cartBll = new BLL.wx_shop_cart();
                string _action = MyCommFun.QueryString("myact");
                //得到微信用户的openid
                string openid = MyCommFun.RequestOpenid();

                Dictionary<string, string> jsonDict = new Dictionary<string, string>();

                if (_action == "LoadProduct")
                {
                    #region 选择排序，改变商品列表
                    string sortColumn = MyCommFun.RequestParam("sortColumn");
                    int wid = MyCommFun.RequestInt("wid");
                    int id = MyCommFun.RequestInt("cid");
                    string name = MyCommFun.RequestParam("name");
                    string flog = MyCommFun.RequestParam("flog");
                    string gd = MyCommFun.RequestParam("gd");
                    DataSet ds = null;
                    BLL.wx_shop_product proBll = new BLL.wx_shop_product();
                    StringBuilder jsonStr = new StringBuilder();
                    if (id != 0)
                    {
                        if (sortColumn == "priceDesc")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " order  by  marketPrice desc ");
                        }
                        if (sortColumn == "priceDesc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " and productName like " + "'%" + name + "%'" + "order  by  marketPrice desc ");
                        }
                        if (sortColumn == "priceAsc")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " order  by  marketPrice asc  ");
                        }
                        if (sortColumn == "priceAsc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " and productName like " + "'%" + name + "%'" + "order  by  marketPrice asc ");
                        }
                        if (sortColumn == "newDesc")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " and latest=" + 1 + " order  by  latest desc  ");
                        }
                        if (sortColumn == "newDesc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " and latest=" + 1 + " and productName like " + "'%" + name + "%'" + "order  by  latest desc ");
                        }
                        if (sortColumn == "saleDesc")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " and hotsale=" + 1 + " order  by  hotsale desc  ");
                        }
                        if (sortColumn == "saleDesc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 20, "categoryId=" + id + condition + " and hotsale=" + 1 + " and productName like " + "'%" + name + "%'" + "order  by hotsale desc ");
                        }
                    }
                    else
                    {
                        if (sortColumn == "priceDesc")
                        {
                            ds = proBll.GetList(wid, 20, "1=1" + condition + " order  by  marketPrice desc ");
                        }
                        if (sortColumn == "priceDesc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 20, " productName like " + "'%" + name + "%'" + condition + "order  by  marketPrice desc ");
                        }
                        if (sortColumn == "priceAsc")
                        {
                            ds = proBll.GetList(wid, 20, " 1=1 " + condition + " order  by  marketPrice asc  ");

                        }
                        if (sortColumn == "priceAsc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 27, " productName like " + "'%" + name + "%'" + condition + "order  by  marketPrice asc ");
                        }
                        if (sortColumn == "newDesc")
                        {
                            ds = proBll.GetList(wid, 20, " latest=" + 1 + condition + " order  by  latest desc  ");
                        }
                        if (sortColumn == "newDesc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 20, " latest=" + 1 + condition + "productName like " + "'%" + name + "%'" + "order  by  latest desc ");
                        }
                        if (sortColumn == "saleDesc")
                        {
                            ds = proBll.GetList(wid, 20, "hotsale=" + 1 + condition + " order  by  hotsale desc  ");
                        }
                        if (sortColumn == "saleDesc" && flog == "true")
                        {
                            ds = proBll.GetList(wid, 20, "hotsale=" + 1 + condition + " and productName like " + "'%" + name + "%'" + "order  by hotsale desc ");
                        }
                    }

                    if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        if (id != 0)
                        {
                            if (gd == "")
                            {
                                foreach (DataRow d in ds.Tables[0].Rows)
                                {
                                    jsonStr.Append(
                                        "<div class=\"lists_items\"> <p class=\"pic\"> <a href=\"/shop/detail.aspx?wid=" + wid + "&pid=" + d["id"] + "&openid=" + openid + "\"> <img src=\"" + d["productpic"] + "\"></a> </p>");
                                    jsonStr.Append(
                                        "<p class=\"list_tex\"><span style=\"color: Black\">" + d["productName"] + "</span><strong style=\"color: Red\">￥" + d["salePrice"] + "</strong></p>");
                                    jsonStr.Append("</div><div style=\"height: 15px; overflow: hidden\"></div>");
                                }
                                jsonStr.Append("<div class=\"details_more\" style=\"float:left; width:100%;\"><A href=\"javascript:void(0);\" id=\"gengduo\" onclick=\"gengduo()\"><span>查看更多</span></A>");
                            }
                            else
                            {
                                foreach (DataRow d in ds.Tables[0].Rows)
                                {
                                    jsonStr.Append("<div class=\"list_msg list_with_img\" style=\"height:100px;margin-top:10px;font-size:12px;\"> <a href=\"/shop/detail.aspx?wid=" + wid + "&pid=" + d["id"] + "&openid=" + openid + "\"><img style=\"top:16px;\" src=\"" + d["productpic"] + "\"></a>");
                                    jsonStr.Append("<p class=\"list_tex\"><span style=\"color: Black\">" + d["productName"] + "</span><strong style=\"color: Red\">￥" + d["salePrice"] + "</strong></p>");
                                    jsonStr.Append("<p class=\"list_tex\"><span class=\"txt_red\" style=\"font-size:12px;color: Black\">" + d["shortDesc"] + "</span></p>");
                                    jsonStr.Append("</div>");
                                }
                            }
                        }
                        else
                        {
                            if (gd == "")
                            {
                                foreach (DataRow d in ds.Tables[0].Rows)
                                {
                                    jsonStr.Append("<div class=\"lists_items\"> <p class=\"pic\"> <a href=\"/shop/detail.aspx?wid=" + wid + "&pid=" + d["id"] + "&openid=" + openid + "\"> <img src=\"" + d["productpic"] + "\"></a> </p>");
                                    jsonStr.Append("<p class=\"list_tex\"><span style=\"color: Black\">" + d["productName"] + "</span><strong style=\"color: Red\">￥" + d["salePrice"] + "</strong></p>");
                                    jsonStr.Append("</div><div style=\"height: 15px; overflow: hidden\"></div>");

                                }
                                jsonStr.Append("<div class=\"details_more\" style=\"float:left; width:100%;\"><A href=\"javascript:void(0);\" id=\"gengduo\" onclick=\"gengduo()\"><span>查看更多</span> </A>");
                            }
                            else
                            {
                                foreach (DataRow d in ds.Tables[0].Rows)
                                {
                                    jsonStr.Append("<div class=\"list_msg list_with_img\" style=\"height:100px;margin-top:10px;font-size:12px;\"> <a href=\"/shop/detail.aspx?wid=" + wid + "&pid=" + d["id"] + "&openid=" + openid + "\"><img style=\"top:16px;\" src=\"" + d["productpic"] + "\"></a>");
                                    // jsonStr.Append("<p class=\"list_tex\"> <span class=\"txt_red\" style=\"font-size:12px;\">￥" + d["salePrice"] + "</span><strong>" + d["productName"] + "</strong></p>");
                                    //jsonStr.Append("<p class=\"list_tex\"><span class=\"txt_red\" style=\"font-size:12px;\">&nbsp</span></p>");
                                    jsonStr.Append("<p class=\"list_tex\"><span style=\"color: Black\">" + d["productName"] + "</span><strong style=\"color: Red\">￥" + d["salePrice"] + "</strong></p>");
                                    jsonStr.Append("<p class=\"list_tex\"><span class=\"txt_red\" style=\"font-size:12px;color: Black\">" + d["shortDesc"] + "</span></p>");
                                    jsonStr.Append("</div>");
                                }
                            }
                        }
                    }

                    context.Response.Write(jsonStr);
                    #endregion
                }
                else if (_action == "sousuo")
                {
                    #region 选择商品名称，改变商品列表

                    string name = MyCommFun.RequestParam("name");
                    int wid = MyCommFun.RequestInt("wid");
                    DataSet ds = null;
                    BLL.wx_shop_product proBll = new BLL.wx_shop_product();
                    StringBuilder jsonStr = new StringBuilder();

                    ds = proBll.GetList(wid, 20, " productName like " + "'%" + name + "%'" + "" + condition);

                    if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow d in ds.Tables[0].Rows)
                        {
                            jsonStr.Append("<div class=\"lists_items\"> <p class=\"pic\"> <a href=\"/shop/detail.aspx?wid=" + wid + "&pid=" + d["id"] + "&openid=" + openid + "\"> <img src=\"" + d["productpic"] + "\"></a> </p>");
                            jsonStr.Append("<p class=\"list_tex\"><span style=\"color: Black\">" + d["productName"] + "</span><strong style=\"color: Red\">￥" + d["salePrice"] + "</strong></p>");
                            jsonStr.Append("</div><div style=\"height: 15px; overflow: hidden\"></div>");
                        }
                        jsonStr.Append("<div class=\"details_more\" style=\"float:left; width:100%;\"><A href=\"javascript:void(0);\" id=\"gengduo\" onclick=\"gengduo()\"><span>查看更多</span><br /></A>");
                    }

                    context.Response.Write(jsonStr);
                    #endregion
                }
                else if (_action == "gengduo")
                {
                    #region 查看更多
                    int wid = MyCommFun.RequestInt("wid");
                    int id = MyCommFun.RequestInt("cid");
                    DataSet ds = null;
                    BLL.wx_shop_product proBll = new BLL.wx_shop_product();
                    StringBuilder jsonStr = new StringBuilder();
                    if (id != 0)
                    {
                        ds = proBll.GetList(wid, -1, "categoryId=" + id + condition + " and p.hotsale=" + 1 + " order  by  p.hotsale desc  ");
                    }
                    else
                    {
                        ds = proBll.GetList(wid, -1, "p.hotsale=" + 1 + condition + " order  by  p.hotsale desc  ");
                    }

                    if (ds != null && ds.Tables[0] != null)
                    {
                        foreach (DataRow d in ds.Tables[0].Rows)
                        {
                            jsonStr.Append("<div class=\"list_msg list_with_img\" style=\"height:100px;margin-top:10px;font-size:12px;\"><a href=\"/shop/detail.aspx?wid=" + wid + "&pid=" + d["id"] + "&openid=" + openid + "\"><img style=\"top:16px;\" src=\"" + d["productpic"] + "\"></a>");
                            //jsonStr.Append("<p class=\"list_tex\"><span class=\"txt_red\" style=\"font-size:12px;\">&nbsp</span></p>");
                            //jsonStr.Append("<p class=\"list_tex\">" + d["productName"] + "<span class=\"txt_red\" style=\"font-size:12px;\">￥" + d["salePrice"] + "</span></p>");
                            jsonStr.Append("<p class=\"list_tex\"><span style=\"color: Black\">" + d["productName"] + "</span><strong style=\"color: Red\">￥" + d["salePrice"] + "</strong></p>");
                            jsonStr.Append("<p class=\"list_tex\"><span class=\"txt_red\" style=\"font-size:12px;color: Black\">" + d["shortDesc"] + "</span></p>");
                            jsonStr.Append("</div>");
                        }
                    }

                    context.Response.Write(jsonStr);
                    #endregion
                }
                else if (_action == "checkid")
                {
                    #region 是否存在该商品
                    jsonDict = new Dictionary<string, string>();
                    int wid = MyCommFun.RequestInt("wid");
                    int id = MyCommFun.RequestInt("id");
                    int skuid = MyCommFun.RequestInt("skuid");
                    BLL.wx_shop_product proBll = new BLL.wx_shop_product();
                    DataSet ds = null;
                    ds = proBll.GetList("wid=" + wid + " and id=" + id + " and upselling<>0");
                    BLL.wx_shop_sku skuBll = new BLL.wx_shop_sku();
                    Model.wx_shop_sku skuModel = skuBll.GetModel(skuid);
                    if (skuid == 0)
                    {
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            jsonDict.Add("data", ds.Tables[0].Rows[0]["stock"].ToString());
                        }
                    }
                    else
                    {
                        jsonDict.Add("data", skuModel.stock.ToString());
                    }

                    context.Response.Write(MyCommFun.getJsonStr(jsonDict));
                    #endregion
                }
            }
            catch (Exception ex)
            {
                //记录日志
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}